define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index :function () {
        // 初始化表格参数配置
        Table.api.init();
        //绑定事件
        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
          var panel = $($(this).attr("href"));
          if (panel.size() > 0) {
            Controller.table[panel.attr("id")].call(this);
            $(this).on('click', function (e) {
              $($(this).attr("href")).find(".btn-refresh").trigger("click");
            });
          }
          //移除绑定的事件
          $(this).unbind('shown.bs.tab');
        });

        //必须默认触发shown.bs.tab事件
        $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
      table: {
        first: function () {
          // 表格1
          var table1 = $("#table1");
          table1.bootstrapTable({
            url: 'store/storage/index',
            extend: {
              index_url: 'store/storage/index',
              add_url: 'order/order/add/',
              edit_url: 'store/storage/edit',
              del_url: 'store/storage/del',
              multi_url: 'store/storage/multi',
              table: 'storage',
            },
            toolbar: '#toolbar1',
            sortName: 'id',
            search: false,
            queryParams: function (params) {
              //这里可以追加搜索条件
              var filter = JSON.parse(params.filter);
              var op = JSON.parse(params.op);
              //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
              filter.depid = groupId;
              op.depid = "in";
              params.filter = JSON.stringify(filter);
              params.op = JSON.stringify(op);
              return params;
            },
            columns: [
              [
                {checkbox: true},
                {field: 'id', title: __('Id')},
                {field: 'preproduct.cname', title: __('Preproduct.cname')},
                {field: 'department.name', title: __('分公司名称')},
                {field: 'stock', title: __('Stock')},
                // {field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
              ]
            ]
          });

          // 为表格1绑定事件
          Table.api.bindevent(table1);
        },
        second: function () {
          // 表格2
          var table2 = $("#table2");
          table2.bootstrapTable({
            url: 'store/storage/index2',
            extend: {
              index_url: '',
              add_url: '',
              edit_url: '',
              del_url: '',
              multi_url: '',
              table: '',
            },
            toolbar: '#toolbar2',
            sortName: 'id',
            search: false,
            queryParams: function (params) {
            //这里可以追加搜索条件
            var filter = JSON.parse(params.filter);
            var op = JSON.parse(params.op);
            //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
            filter.depid = groupId;
            op.depid = "in";
            params.filter = JSON.stringify(filter);
            params.op = JSON.stringify(op);
            return params;
          },
            columns: [
              [
                {checkbox: true},
                {field: 'id', title: __('Id')},
                {field: 'preproduct.cname', title: __('Preproduct.cname')},
                {field: 'department.name', title: __('分公司名称')},
                {field: 'stock', title: __('Stock')},
                // {field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
              ]
            ]
          });

          // 为表格2绑定事件
          Table.api.bindevent(table2);
        }
      },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});